import matplotlib.pyplot as plt
import numpy as np
import math

Est_pre = 0           #设定初始的估计值
X_measure = [0]*4      #测量值初始化
Est = [0]*4

print('请输入：')
for i in range(0,4):
    a = input()
    a = float(a)
    X_measure[i] = a
print('输入内容是',X_measure)

T = 1.0                  #采样时间
k = [1,2,3,4]          #采样次数
K = [0.0]*4              #滤波器的增益
#y = [1.2,0.2,2.9,2.1] #采样的测量值
t = [0]*len(K)
for i in range(0,len(k)):
    t[i] = (k[i]-1)*T

#0阶滤波器的计算
duibi = (1/len(k))*sum(X_measure)

#0阶迭代最小二乘滤波的计算
for i in range(0,len(k)):
    K[i] = 1/k[i]
    Est[i] = Est_pre + K[i]*(X_measure[i]-Est_pre)
    Est_pre = Est[i]

plt.xlabel("Time(Sec)")
plt.ylabel("Est")
plt.plot(t,X_measure,'+')
plt.plot(t,Est,'--',label = 'zero-order least squares filters')
plt.axhline(y=duibi, c="k", ls="--", lw=2,label = 'zero-order')
plt.legend()
plt.show()


